<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <!-- default-expand-all="true" 控制默认展开所有子节点 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
              <el-col :span="20">
                <svg-icon icon-class="baobing" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title=" isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        @closed="hClose"
      >
        <AddorEdit
          :id="curId"
          ref="depDialogRef"
          :is-edit="isEdit"
          :origin-list="originList"
          @renovated="hRenovated"
          @close="showDialog=false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartment, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  components: { AddorEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      isEdit: false, // false表示添加子部门,true表示编辑部门
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 将数据改成树状结构方便渲染
    async loadDepartments() {
      try {
        const res = await getDepartment()
        console.log(res)
        this.originList = res.data.depts.map(({ id, pid, code, name }) => { return { id, name, code, pid } })
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
        const root = res.data.depts.shift()
        console.log(root)
        // 用工具函数把 平铺的数组转成 树型数据
        this.list = tranListToTreeData(res.data.depts)
      } catch (err) {
        console.log(err)
      }
    },

    // 点击添加子部门获取大部门的data.id传给子组件
    hAdd(id) {
      // 控制弹窗开启
      this.showDialog = true
      // 将获取到的id保存
      this.curId = id
      // 改变isEdit的布尔值控让子组件知道是什么操作
      this.isEdit = false
    },

    // 点击编辑部门获取大部门的data.id传给子组件
    hEdit(id) {
      // 控制弹窗开启
      this.showDialog = true
      // 将获取到的id保存
      this.curId = id
      // 改变isEdit的布尔值控让子组件知道是什么操作
      this.isEdit = true
      // 通过引用子组件,调用子组件的loadDepartmentsDetail方法
      // 重新获取数据,重新渲染
      this.$nextTick(() => {
        this.$refs.depDialogRef.loadDepartmentsDetail()
      })
    },

    // 点击删除部门
    hDel(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 确认
        try {
          await delDepartments(id)
          this.loadDepartments()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        // 取消
      })
    },

    // 子组件通知更新数据并关闭弹窗的函数
    hRenovated() {
      // 关闭弹窗
      this.showDialog = false
      // 更新数据
      this.loadDepartments()
    },

    // 重置表单
    hClose() {
      this.$refs.depDialogRef.resetForm()
    }
  }
}
</script>
